package com.study.talk.ui.app.friend

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.study.talk.ui.adapter.UsersAdapter
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.databinding.ActivityAddFriendBinding
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AddFriendActivity : BaseActivity() {

    private val binding: ActivityAddFriendBinding by lazy {
        ActivityAddFriendBinding.inflate(
            layoutInflater
        )
    }

    private val vm : AddFriendViewModel by viewModels()

    private val adapter : UsersAdapter by lazy {
        UsersAdapter(UsersAdapter.DETAIL)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setSupportActionBar(binding.addFriendToolbar)
        initView()
    }


    private fun initView() {
        binding.addFriendListview.layoutManager = LinearLayoutManager(this)
        binding.addFriendListview.adapter = adapter
        binding.addFriendToolbar.setNavigationOnClickListener { finish() }
        binding.addFriendSearch.setOnQueryTextListener(object :
            SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                if (query.isEmpty()) {
                    showToast("请输入搜索内容")
                    return false
                }
                vm.queryUsers(query)
                return true
            }

            override fun onQueryTextChange(newText: String): Boolean {
                return false
            }
        })
        vm.users.observe(this){//列表发生变化时更新布局
            adapter.submitList(it)
        }
    }
}